<?php

require_once 'Zend/Controller/Plugin/Abstract.php';

class Bugapp_Plugin_Initialize extends Zend_Controller_Plugin_Abstract {
	/**
	 * @var Zend_Config
	 */
	protected static $_config;
	
	/**
	 * @var string Current environment
	 */
	protected $_env;
	
	/**
	 * @var Zend_Controller_Front
	 */
	protected $_front;
	
	/**
	 * @var string Path to application root
	 */
	protected $_root;
	
	/**
	 * Constructor
	 *
	 * Initialize environment, root path, and configuration.
	 *
	 * @param  string $env
	 * @param  string|null $root
	 * @return void
	 */
	public function __construct($env, $root = null) {
		$this->_setEnv ( $env );
		if (null === $root) {
			$root = realpath ( dirname ( __FILE__ ) . '/../../../TV/' );
		}
		$this->_root = $root;
		
		//$this->initPhpConfig();
		

		$this->_front = Zend_Controller_Front::getInstance ();
	}
	
	private function _setEnv($env) {
		$this->_env = $env;
	}
	
	/**
	 * Route startup
	 *
	 * @return void
	 */
	public function routeStartup(Zend_Controller_Request_Abstract $request) {
		//$this->initDb();
		//$this->initHelpers();
		$this->initView ();
		//$this->initPlugins();
		$this->initRoutes ();
		$this->initModules ();
		//$this->initControllers();
	}
	
	public function initView() {
		$ctrl = Zend_Controller_Front::getInstance ();
		
		$view = new TV_View_Smarty ( );
		
		$viewRenderer = new Zend_Controller_Action_Helper_ViewRenderer ( $view );
		
		$viewRenderer->setViewScriptPathSpec ( ':controller/:action.:suffix' )->setViewScriptPathNoControllerSpec ( ':action.:suffix' )->setViewSuffix ( 'tpl' );
		
		Zend_Controller_Action_HelperBroker::addHelper ( $viewRenderer );
	
	}
	
	public function initRoutes() {
		/*$ctrl = Zend_Controller_Front::getInstance ();
		$router = $ctrl->getRouter ();
		$route = new Zend_Controller_Router_Route ( ':module/:controller/:id', array ('module' => 'default', 'controller' => 'index', 'action' => 'index', 'id' => 'id' ) );
		$router->addRoute ( 'idParam2indexAction', $route );
		
		$route = new Zend_Controller_Router_Route ( 'ajax/:serviceName/:serviceAction', array ('module' => 'default', 'controller' => 'ajax', 'action' => 'index', 'serviceName' => 'serviceName', 'serviceAction' => 'serviceAction' ) );
		$router->addRoute ( 'serviceRoot', $route );*/
	
	}
	
	public function initModules() {
		$ctrl = Zend_Controller_Front::getInstance ();
		$ctrl->setControllerDirectory($this->_root.'/Controller');
		
	
	}

}